<?php
declare (strict_types=1);

namespace app\common\queue\school;

use app\school\logic\school\admin\SchoolDomain;

/**
 * 机构自定义域名CNAME解析检测
 */
class SchoolDomainCheck
{
    public function run()
    {
        // 查询附件
        $map = [
            ['node', '=', 2]
        ];

        /** @var SchoolDomain $schoolDomainLogic */
        $schoolDomainLogic = app(SchoolDomain::class);

        $buildSql = $schoolDomainLogic->field('max(`id`)')->group('mhm_id')->buildSql();

        $domains = $schoolDomainLogic
            ->where($map)
            ->whereRaw(sprintf('`id` IN %s', $buildSql))
            ->order('create_time', 'DESC')
            ->field(['id', 'mhm_id', 'domain'])
            ->select();

        if ($domains->isEmpty()) return;

        try {
            foreach ($domains as $domain) {
                // 获取cname状态
                $status = $schoolDomainLogic->getCnameStatus($domain->domain, $domain->mhm_id);
                SchoolDomain::update(['cname_status' => $status], ['id' => $domain->id, 'mhm_id' => $domain->mhm_id]);
            }
        } catch (\Exception $exception) {
        }
    }
}
